function observe(obj) {
  if (obj == null || typeof obj !== 'object') {
    return;
  }
  new Observe(obj);
}

function defineReactive(obj, key, value) {
  new Observe(value);

  const dep = new Dep();
  Object.defineProperty(obj, key, {
    get() {
      // 收集依赖
      if (Dep.target) {
        dep.addDep(Dep.target);
      }
      return value;
    },
    set(newVal) {
      if (value !== newVal) {
        observe(newVal);
        value = newVal;
        //  通知依赖更新
        dep.notify();
      }
    },
  });
}

class Observe {
  constructor(obj) {
    if (Array.isArray(obj)) {
      console.log('数组');
    } else if (obj != null && typeof obj === 'object') {
      this.walk(obj);
    }
  }

  walk(obj) {
    Object.keys(obj).forEach((key) => {
      defineReactive(obj, key, obj[key]);
    });
  }
}
